package com.tongzhou.tongzhou.quot.action;

import java.io.File;
import java.util.List;
import java.util.TreeMap;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.quot.dao.IPaperTypeCoreDAO;
import com.tongzhou.tongzhou.quot.model.PaperCore;
import com.tongzhou.tongzhou.quot.model.PaperTypeCore;
import com.tongzhou.tongzhou.quot.model.SearchPaperTypeCoreCondition;
import com.tongzhou.util.FileUtil;

public class PaperTypeCoreAction extends ActionSupport{
	private int id;
	private List<PaperTypeCore> result=null;
	private SearchPaperTypeCoreCondition condition=null;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private String type;
	private String create_time;
	
	private IPaperTypeCoreDAO dao=null;
	private List<PaperCore> paperCores1=null;
	private List<PaperCore> paperCores2=null;
	private File quoturl ;
	/**
	 * 加芯&中隔列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listPaperTypeCore() throws DBException{
		checkSearchCondition();
		condition = new SearchPaperTypeCoreCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
		condition.setType(type);
	    result = dao.listPaperTypeCore(condition);
	    PaperTypeCore p=result.get(0);
	    create_time=p.getCreate_time();
		return "listPaperTypeCore";
	}
	
	public String listPaperCore() throws DBException{
		paperCores1=dao.getPaperCoreList("1");
		paperCores2=dao.getPaperCoreList("2");
		return "listPaperCore";
	}
	
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	
	/**
	 * 新增加芯&中隔
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddPaperTypeCore() throws DBException{
		return "addPaperTypeCore";
	}
	
	public String addPaperTypeCore() throws DBException{
		dao.deletePaperTypeCore();
		dao.updatePaperTypeCore();
		uploadClxxFile();
		listPaperTypeCore();
		return "listPaperTypeCore";
	}
	
	/**
	 * 解析excel
	 */
	public void uploadClxxFile(){
		try {
			if(quoturl!=null){
				HSSFSheet sheet=FileUtil.readExcel(quoturl);
				uploadClxxApp(sheet);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ;
		}
	}
	
	public void uploadClxxApp(HSSFSheet sheet) throws Exception {
		int FirstRowNum = sheet.getFirstRowNum();
		int LastRowNum = sheet.getLastRowNum();
		try {
			int j=0;
			TreeMap<Integer,String> map=new TreeMap<Integer,String>();
			HSSFRow row2 = sheet.getRow(FirstRowNum);
			if (row2 != null) {
				HSSFCell cell = row2.getCell(j+1);
				while(cell!=null){
					String paperCoreName = FileUtil.getHSSFCellValue(cell);
					map.put(j, paperCoreName);
					j++;
					cell = row2.getCell(j+1);
				}
			}
			
			HSSFRow row = sheet.getRow(1);
			if (row != null) {
				for(int m=1;m<j+1;m++){
					PaperCore paperCore=new PaperCore();
					HSSFCell cell1 = row.getCell(m);
					String price = FileUtil.getHSSFCellValue(cell1);
					paperCore.setPaper_core_name(map.get(m-1));
					paperCore.setPrice(price);
					paperCore.setPaper_type_core("1");
					dao.addPaperCore(paperCore);
				}
			}
			

			HSSFRow row3 = sheet.getRow(2);
			if (row3 != null) {
				for(int m=1;m<j+1;m++){
					PaperCore paperCore=new PaperCore();
					HSSFCell cell1 = row3.getCell(m);
					String price = FileUtil.getHSSFCellValue(cell1);
					paperCore.setPaper_core_name(map.get(m-1));
					paperCore.setPrice(price);
					paperCore.setPaper_type_core("2");
					dao.addPaperCore(paperCore);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("插入数据时发生异常");
		}
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public List<PaperTypeCore> getResult() {
		return result;
	}


	public void setResult(List<PaperTypeCore> result) {
		this.result = result;
	}


	public SearchPaperTypeCoreCondition getCondition() {
		return condition;
	}


	public void setCondition(SearchPaperTypeCoreCondition condition) {
		this.condition = condition;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public String getOrderBy() {
		return orderBy;
	}


	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}


	public String getOrderByType() {
		return orderByType;
	}


	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}


	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}


	public IPaperTypeCoreDAO getDao() {
		return dao;
	}


	public void setDao(IPaperTypeCoreDAO dao) {
		this.dao = dao;
	}


	public File getQuoturl() {
		return quoturl;
	}


	public void setQuoturl(File quoturl) {
		this.quoturl = quoturl;
	}

	public String getCreate_time() {
		return create_time;
	}

	public void setCreate_time(String createTime) {
		create_time = createTime;
	}

	public List<PaperCore> getPaperCores1() {
		return paperCores1;
	}

	public void setPaperCores1(List<PaperCore> paperCores1) {
		this.paperCores1 = paperCores1;
	}

	public List<PaperCore> getPaperCores2() {
		return paperCores2;
	}

	public void setPaperCores2(List<PaperCore> paperCores2) {
		this.paperCores2 = paperCores2;
	}
}
